#ifndef SERIALMANAGER_H
#define SERIALMANAGER_H

#include <QObject>
#include <QSerialPort>
#include <QString>
#include <QDebug>
#include <QThread>
#include <QByteArray>
#include <QTimer>
#include <QMutex>
#include <QMutexLocker>
#include <QStringList>
class SerialManager : public QObject
{
    Q_OBJECT
public:
    explicit SerialManager(QObject *parent = 0);
    ~SerialManager();


    void openPort(QString name)
    {
        emit signalOpenPort(name);
    }

    void closePort()
    {
        emit signalClosePort();
    }

    void sendData(QByteArray data)
    {
        emit signalSend(data);
    }

    void setPortBaudRate(QString param);
    void setPortStopBits(QSerialPort::StopBits stopBits);
    void setPortDataBits(QSerialPort::DataBits dataBits);
    void setPortParity(QSerialPort::Parity parity);
    void setTimeStamp(bool newState);
    void setAlignment(int len);
    void clearData();
    quint64 getRecCount();

signals:
    void error(int error,QString msg);
    void signalOpenPort(QString name);
    void signalClosePort();

    void update_data( QByteArray data_hex, QByteArray data_str);
    void signalSend(QByteArray data);


public slots:
    void onOpenPort(QString name);
    void onClosePort();
    void onTimer1();
    void onReadyRead();
    void onTransmit(QByteArray data);


private:
    QSerialPort *comPort;
    QTimer  *timer1;
    QString portName;
    QString currentPortName;
    QByteArray str_to_post;
    QByteArray hexstr_to_post;
    quint64  rec_total;

    QMutex mutexPort;
    QMutex mutexdata;

    bool btimeStamp;
    int alignLen;
    int lenCnt;
};

#endif // SERIALMANAGER_H
